package com.dp.appi.dao.impl;

import com.dp.appi.dao.PayDao;
import com.dp.appi.util.DateUtil;
import com.dp.appi.util.StringUtil;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * Created by bchen on 2016/6/20.
 */
@Component
public class PayDaoImpl extends BaseDaoImpl implements PayDao{
    /**
     * 平台产品购买清单
     *
     * @return
     */
    @Override
    public List<Map<String, Object>> buyMenuList(Integer appliesType) {
        List list = new ArrayList();
        StringBuffer sql = new StringBuffer();
        sql.append("select `buy_menu_id`, `name`, `vaild_time`, `time_unit`,");
        sql.append("`per_averages`, `products_price`,");
        sql.append(" `applies_type`, `illustrate`,`def_people_num`,");
        sql.append(" DATE_FORMAT(`create_time`,'%Y-%m-%d %H:%i:%s') create_time");
        sql.append(" from b_buy_menu where is_valid = 1");
        sql.append(" and deleted = 0");
        if(appliesType != null){
            list.add(appliesType);
            sql.append(" and applies_type=?");
        }
        sql.append(" ORDER BY create_time DESC");
        return getJdbcTemplate().queryForList(sql.toString(),list.toArray());
    }

    @Override
    public Map<String, Object> getBuyMent(Long buyMentId, Integer appliesType) {
        String sql = "select t1.* from b_buy_menu as t1 where t1.is_valid=1 and t1.deleted=0 and t1.buy_menu_id=? " +
                "and t1.applies_type=?";

        return findOneObject(sql, new Object[]{buyMentId, appliesType});
    }

    @Override
    public void addBuyLog(String user_id, String total_fee, String type, String people_num, String departid, Map<String, Object> buyMentMap) {
        String sql = "insert into b_buy_log(buyer,total_price,products_price,per_averages,applies_type,people_num,vaild_time,time_unit,org_id) " +
                "values(?,?,?,?,?,?,?,?,?)";
        executeSql(sql, new Object[]{user_id,total_fee,buyMentMap.get("products_price"),buyMentMap.get("per_averages"),
        type, people_num, buyMentMap.get("vaild_time"), buyMentMap.get("time_unit"), departid});
    }

    @Override
    public void upUser(String vaild_time, String time_unit, String start_time, String user_id) {
        // 先判断该用户之前有没有冲值，
        // 如果冲值又没有到期时就直接在到期日期上加日期
        // 如果冲值了到期了就直接修改
        String sql = "select vip_end from t_s_user where id=?";
        Map<String, Object> map = findOneObject(sql, new Object[]{user_id});
        Object vip_end = map.get("vip_end");
        if(map != null && StringUtil.isNotEmpty(vip_end)){ //之前购买过
            // 判断是不是续期，如果是续期就直接在到时日期上加，如果不是就直接修改
            int res = DateUtil.compareStrTo(vip_end.toString(), start_time, "yyyy-MM-dd");
            if(res >= 0){ // 续期
                String end_time = DateUtil.calculateFutureDate(vaild_time, time_unit, vip_end.toString());
                String sql2 = "update t_s_user set is_vip=1, vip_end=? where id=?";
                executeSql(sql2, new Object[]{end_time, user_id});
            }else{
                String end_time = DateUtil.calculateFutureDate(vaild_time, time_unit, start_time);
                String sql2 = "update t_s_user set is_vip=1, vip_start=?, vip_end=? where id=?";
                executeSql(sql2, new Object[]{start_time, end_time, user_id});
            }
        }else {//之前没有购买过
            String end_time = DateUtil.calculateFutureDate(vaild_time, time_unit, start_time);
            String sql2 = "update t_s_user set is_vip=1, vip_start=?, vip_end=? where id=?";
            executeSql(sql2, new Object[]{start_time, end_time, user_id});
        }
    }

    @Override
    public void upRoleUser(String user_id) {
        String sql = "delete from t_s_role_user where userid=?";
        executeSql(sql, new Object[]{user_id});

        UUID uuid = UUID.randomUUID();
        String id = uuid.toString().replaceAll("-", "");
        sql = "insert into t_s_role_user(ID, roleid, userid) values(?,?,?)";
        executeSql(sql, new Object[]{id, "ff80808155bef7320155bf3d5bc40018", user_id});
    }

    @Override
    public void upDepart(String departid, String people_num, String vaild_time, String time_unit, String finish_time) {
        String end_time = DateUtil.calculateFutureDate(vaild_time, time_unit, finish_time);
        String sql = "update t_s_depart set people_limit=?, start_time=?, end_time=? where ID=?";
        executeSql(sql, new Object[]{people_num, finish_time, end_time, departid});
    }
}
